const vm = require('vm');
const context = { x: 2, setTimeout, clearTimeout }; // 上下文中，没有 setTimeout 和 clearTimeout 函数……
vm.createContext(context); // contextify the object

const code = `
const sleep = (ms) => new Promise((resolve) => {
    const tot = setTimeout(() => {
        resolve();
        clearTimeout(tot);
    }, ms);
});

(async () => {
    console.log("xxxx"); // 没有任何输出
    await sleep(1000);
    x += 10
    console.log(x);   // 没有任何输出
    return x
})()
`;

(async () => {
  const promise = vm.runInContext(code, context);
  console.log('xx ------ ' + promise);
  console.log(context.x);

  const result = await promise;
  console.log('xx ------ ' + result);
})();
